import pymysql

db = pymysql.connect(host='localhost',user='root',password='123456',port=3306,db='spiders')
cursor = db.cursor()
sql = 'SELECT * FROM students WHERE age >= 20'
try:
    cursor.execute(sql)
    print("Count:",cursor.rowcount)
    row = cursor.fetchone()
    while row:
        print("Row",row)
        row = cursor.fetchone()
except Exception as e:
    print("Failed")
    print(e)
db.close()

'''
分析：fetchall会将结果以元组形式全部返回，如果数据量很大，那么占用的开销会非常高
    推荐使用while循环+fetchone()方法来获取所有数据
        每循环一次，指针就会偏移一条数据，随用随取，简单高效
'''